##################################################################
# Paper Title:    Anger, Fear, and The Racialization of News 
#                      Media Coverage of Protest Activity 	 			  	 			          
# Authors: 	LaGina Gause  (lgause@ucsd.edu) 
# 		    	Steven Moore  (stmoore@wesleyan.edu)
#		      	Mara Cecilia Ostfeld  (mostfeld@umich.edu)
# Date:       8/1/2023                                                       
# Software:   R Studio Version 2023.03.0+386 (2023.03.0+386)                                                 
##################################################################

Needed <- c("dplyr", "ggthemes", "ggeffects", "ggplot2", "tidytext", "tidyr", "magrittr")   
install.packages(Needed, dependencies=TRUE)   
install.packages("tidytext")
install.packages("textdata")
install.packages("readxl")

library(dplyr)
library(ggthemes)
library(ggeffects)
library(ggplot2)
library(tidytext)
library(magrittr)
library(tidyr)
library(janeaustenr)
library(textdata)
library(readxl)

#upload data
pdata=read.csv(file.choose())
#jrep_mediabias_figure1.csv
##clean protest data w tone & emotion & police22.csv


############# Graph for Non White, Liberal Protests ############# 

#grouping text
NONWHITE=pdata[pdata$NONWHITE==1, ] # sub setting to nonwhite protests 
NONWHITE$text=as.character(NONWHITE$text) # transforming text into character type
nw=unnest_tokens(NONWHITE, word, text, token="words") # breaks down text variable into individual words, making each word an observation 


#### Get fear words from NRC dictionary from Tidy Text
narc <- get_sentiments("nrc") %>% 
  filter(sentiment == "fear")

#Inner Join 
nwcounts <-nw %>%
  inner_join(narc) %>%
  count(word, sentiment, sort = TRUE) %>%
  ungroup()

# Graph 
nwcounts %>%
  group_by(sentiment) %>%
  top_n(10) %>%
  ungroup() %>%
  mutate(word = reorder(word, n)) %>%
  ggplot(aes(word, n, fill = sentiment)) +
  geom_col(show.legend = FALSE, colour = "black", fill = "dark gray") +
  facet_wrap(~sentiment, scales = "free_y") +
  labs(title = "(A) Fear-Inducing Words in Non-White Protests", y = "Mentions",
       x = "Words") +
  ylim(NA,4000)+
  coord_flip()



#### Get ANGER words from NRC dictionary from Tidy Text
narc <- get_sentiments("nrc") %>% 
  filter(sentiment == "anger")

#Inner Join 
nwcounts <-nw %>%
  inner_join(narc) %>%
  count(word, sentiment, sort = TRUE) %>%
  ungroup()

# Graph 
nwcounts %>%
  group_by(sentiment) %>%
  top_n(10) %>%
  ungroup() %>%
  mutate(word = reorder(word, n)) %>%
  ggplot(aes(word, n, fill = sentiment)) +
  geom_col(show.legend = FALSE, colour = "black", fill = "dark grey") +
  facet_wrap(~sentiment, scales = "free_y") +
  labs(title = "(B) Anger-Inducing Words in Non-White Protests", y = "Mentions",
       x = "Words") +
  ylim(NA,4000)+
  coord_flip()

############# Graph for White, Liberal Protests ############# 

#grouping text 
WHITE=pdata[pdata$WHITE==1, ]
WHITE$text=as.character(WHITE$text)
w=unnest_tokens(WHITE, word, text, token="words")

#### Get FEAR words from NRC dictionary from Tidy Text

narc <- get_sentiments("nrc") %>% 
  filter(sentiment == c("fear"))

wcounts <- w %>%
  inner_join(narc) %>%
  count(word, sentiment, sort = TRUE) %>%
  ungroup()

wcounts %>%
  group_by(sentiment) %>%
  top_n(10) %>%
  ungroup() %>%
  mutate(word = reorder(word, n)) %>%
  ggplot(aes(word, n, fill = sentiment)) +
  geom_col(show.legend = FALSE, colour = "black", fill = "dark gray") +
  facet_wrap(~sentiment, scales = "free_y") +
  labs(title = "(C) Fear-Inducing Words in White Protests", y = "Mentions",
       x = "Words") +
  ylim(NA,4000)+
  coord_flip()

#### Get ANGER words from NRC dictionary from Tidy Text

narc <- get_sentiments("nrc") %>% 
  filter(sentiment == c("anger"))

wcounts <- w %>%
  inner_join(narc) %>%
  count(word, sentiment, sort = TRUE) %>%
  ungroup()

wcounts %>%
  group_by(sentiment) %>%
  top_n(10) %>%
  ungroup() %>%
  mutate(word = reorder(word, n)) %>%
  ggplot(aes(word, n, fill = sentiment)) +
  geom_col(show.legend = FALSE, colour = "black", fill = "dark grey") +
  facet_wrap(~sentiment, scales = "free_y") +
  labs(title = "(D) Anger-Inducing Words in White Protests", y = "Mentions",
       x = "Words") +
  ylim(NA,4000)+
  coord_flip()
